package com.jzg.jcptadmin.presenter;

import com.jzg.jcptadmin.app.AppContext;
import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.DataManager;
import com.jzg.jcptadmin.data.vo.ReplyInfo;
import com.jzg.jcptadmin.viewinterface.FastOnlineActivityInterface;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2016/2/22.
 */
public class FastOnlinePresenter extends BasePresenter<FastOnlineActivityInterface> {
    private AppContext con;
    private DataManager mDataManager;
    private Subscription mSubscription;
    public FastOnlinePresenter(AppContext con, DataManager instance) {
        this.con = con;
        mDataManager = instance;
    }
    public void saveReply(String carId, String des) {
        mSubscription = mDataManager.UploadReplay(con.getUser().getUserId() + "", carId, des).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Observer<ReplyInfo>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e != null&&getMvpView()!=null) {
                    getMvpView().showError(e.getMessage());
                } else {
                    getMvpView().showError("网络请求失败，请检查您的网络");
                }
            }

            @Override
            public void onNext(ReplyInfo data) {
                getMvpView().saveReply(data);
            }
        });
    }

    @Override
    public void detachView() {
        super.detachView();
        if (mSubscription != null)
            mSubscription.unsubscribe();
    }
}
